package com.example.signatureboard;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class SignatureView extends View {
    private Paint paint;
    private Path currentPath;
    private List<Path> paths;
    private List<Paint> paints;
    private Canvas canvas;
    private Bitmap bitmap;
    private Paint bitmapPaint;

    public SignatureView(Context context) {
        super(context);
        init();
    }

    public SignatureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SignatureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(8f);

        currentPath = new Path();
        paths = new ArrayList<>();
        paints = new ArrayList<>();
        bitmapPaint = new Paint(Paint.DITHER_FLAG);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        canvas.drawBitmap(bitmap, 0, 0, bitmapPaint);
        
        // Draw all saved paths
        for (int i = 0; i < paths.size(); i++) {
            canvas.drawPath(paths.get(i), paints.get(i));
        }
        
        // Draw current path
        canvas.drawPath(currentPath, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                currentPath.reset();
                currentPath.moveTo(x, y);
                break;
            case MotionEvent.ACTION_MOVE:
                currentPath.lineTo(x, y);
                break;
            case MotionEvent.ACTION_UP:
                // Save the current path and paint
                paths.add(new Path(currentPath));
                Paint newPaint = new Paint(paint);
                paints.add(newPaint);
                
                // Draw to bitmap
                this.canvas.drawPath(currentPath, paint);
                
                // Reset current path
                currentPath.reset();
                break;
        }
        
        invalidate();
        return true;
    }

    public void clearSignature() {
        paths.clear();
        paints.clear();
        currentPath.reset();
        
        if (bitmap != null) {
            canvas.drawColor(Color.WHITE);
        }
        
        invalidate();
    }

    public void undoLastStroke() {
        if (!paths.isEmpty()) {
            // Remove the last path and paint
            paths.remove(paths.size() - 1);
            paints.remove(paints.size() - 1);
            
            // Redraw the bitmap
            if (bitmap != null) {
                canvas.drawColor(Color.WHITE);
                for (int i = 0; i < paths.size(); i++) {
                    canvas.drawPath(paths.get(i), paints.get(i));
                }
            }
            
            invalidate();
        }
    }

    public boolean canUndo() {
        return !paths.isEmpty();
    }

    public Bitmap getSignatureBitmap() {
        if (bitmap == null) {
            return null;
        }
        
        // Create a new bitmap with the current drawing
        Bitmap signatureBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas signatureCanvas = new Canvas(signatureBitmap);
        
        // Fill with white background
        signatureCanvas.drawColor(Color.WHITE);
        
        // Draw all paths
        for (int i = 0; i < paths.size(); i++) {
            signatureCanvas.drawPath(paths.get(i), paints.get(i));
        }
        
        return signatureBitmap;
    }

    public boolean isEmpty() {
        return paths.isEmpty();
    }

    public void setStrokeWidth(float width) {
        paint.setStrokeWidth(width);
    }

    public void setStrokeColor(int color) {
        paint.setColor(color);
    }
}